//
// Created by HP on 2022/2/1.
//

#include "simple_render_system.h"

namespace tmc {
    void SimpleRenderSystem::renderGameObjects(
            std::vector<TmcGameObject> &gameObjects, const tmc::TmcCamera &camera) {
        shaderProgram.use();
        const auto projectionMatrix = camera.getProjection();
        const auto viewMatrix = camera.getView();
        glClearColor(0.1, 0.1, 0.1, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        for (const auto& gameObj: gameObjects) {
            auto modelMatrix = gameObj.transform.mat4();
            auto normalMatrix = gameObj.transform.normalMatrix();
            shaderProgram.setMat4("projectionMatrix", projectionMatrix);
            shaderProgram.setMat4("viewMatrix", viewMatrix);
            shaderProgram.setMat4("modelMatrix", modelMatrix);
            shaderProgram.setMat4("normalMatrix", normalMatrix);
            gameObj.model->bind();
            gameObj.model->draw();
        }
    }

    SimpleRenderSystem::SimpleRenderSystem(const std::string &vertShaderPath, const std::string &fragShaderPath) {
        shaderProgram = {
            tmc::Shader::createShaderFromFile(Shader::ShaderType::Vert, vertShaderPath),
            tmc::Shader::createShaderFromFile(Shader::ShaderType::Frag, fragShaderPath),
        };
        shaderProgram.compileShaderProgram();
    }
}
